emojichooser: Make keynav work with != 7 columns
authorMatthias Clasen <mclasen@redhat.com>
Tue, 22 Dec 2020 03:44:28 +0000 (22:44 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 26 Jan 2021 04:32:08 +0000 (23:32 -0500)
The number of 7 columns was hardcoded in a few places
related to keynav across sections. The flowbox does
not have an api for it, but we can find out anyway
how many columns there are.

gtk/gtkemojichooser.c

index 6625599942994387f664a8ca4a5dc01734596f26..2f5d223ccdbe25a82277b728ab6f421ac41ad926 100644 (file)
@@ -1081,16 +1081,35 @@ keynav_failed (GtkWidget        *box,
                GtkEmojiChooser  *chooser)
 {
   EmojiSection *next;
-  GtkWidget *focus; 
+  GtkWidget *focus;
   GtkWidget *child;
   GtkWidget *sibling;
   int i;
   int column;
+  int n_columns = 7;
+  int child_x;
 
   focus = gtk_root_get_focus (gtk_widget_get_root (box));
   if (focus == NULL)
     return FALSE;
 
+  /* determine the number of columns */
+  child_x = -1;
+  for (i = 0; i < 20; i++)
+    {
+      GtkAllocation alloc;
+
+      gtk_widget_get_allocation (GTK_WIDGET (gtk_flow_box_get_child_at_index (GTK_FLOW_BOX (box), i)),
+                                 &alloc);
+      if (alloc.x > child_x)
+        child_x = alloc.x;
+      else
+        {
+          n_columns = i;
+          break;
+        }
+    }
+
   child = gtk_widget_get_ancestor (focus, GTK_TYPE_EMOJI_CHOOSER_CHILD);
 
   i = 0;
@@ -1099,7 +1118,7 @@ keynav_failed (GtkWidget        *box,
        sibling = gtk_widget_get_next_sibling (sibling))
     i++;
 
-  column = i % 7;
+  column = i % n_columns;
 
   if (direction == GTK_DIR_DOWN)
     {
@@ -1131,7 +1150,7 @@ keynav_failed (GtkWidget        *box,
            sibling;
            sibling = gtk_widget_get_next_sibling (sibling), i++)
         {
-          if ((i % 7) == column)
+          if ((i % n_columns) == column)
             child = sibling;
         }
       if (child)